package com.example.demo.jucConcurrent.Semaphore详解;

import java.util.concurrent.Semaphore;

public class SemaphoreDemo {

    public  final  static  int SEM_SIZE = 10;

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(SEM_SIZE);
        MyThread t1 = new MyThread("t1", semaphore);
        MyThread t2 = new MyThread("t2", semaphore);
        t1.start();
        t2.start();
        int  permits = 5;
        System.out.println(Thread.currentThread().getName());
        try {
            semaphore.acquire(permits);
            System.out.println(Thread.currentThread().getName()+"acquire successfully");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
            System.out.println(Thread.currentThread());
        }

    }
}

class MyThread extends  Thread{
    private  Semaphore semaphore;

    public MyThread(String name,Semaphore semaphore) {
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        int count = 3;
        System.out.println(Thread.currentThread().getName());

        try {
            semaphore.acquire(count);
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semaphore.release(count);
            System.out.println(Thread.currentThread().getName());
        }
    }
}
